/*
 * Copyright (C) 2011  Xu Haojie
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.poemcode.gzlib.detail;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;

import net.poemcode.gzlib.common.CommonThread;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class DetailThread extends CommonThread {
	
	public static final int MSG_DETAIL_ID = 0;
	
	private static final String TAG = "DetailThread";
	
	private Context context;
	
	public DetailThread(Context context, Handler handler, DetailRequest request) {
		super(handler, request);
		this.context = context;
	}

	@Override
	public void run() {
		Message msg = mHandler.obtainMessage(DetailThread.MSG_DETAIL_ID);
		
		HttpClient httpclient = new DefaultHttpClient();
		URI uri = null;
		try {
			uri = new URI(BASE_URL + "?" +URLEncodedUtils.format(mRequest.getHttpParamemters(), "UTF-8"));
		} catch (URISyntaxException e) {
			Log.e(TAG, e.getMessage());
		}
		
		HttpGet httpget = new HttpGet(uri);
		HttpResponse response;
		try {
			response = httpclient.execute(httpget);
		} catch (ClientProtocolException e) {
			Log.e(TAG, e.getMessage());
			msg.sendToTarget();
			return;
		} catch (IOException e) {
			Log.e(TAG, e.getMessage());
			msg.sendToTarget();
			return;
		}
		
		List<DetailResult> result = new LinkedList<DetailResult>();
		int status = response.getStatusLine().getStatusCode();
		if (status == HttpStatus.SC_OK) {
			try {
				result = DetailPullParser.getDetailList(context, response.getEntity().getContent());
			} catch (IllegalStateException e) {
				result = new LinkedList<DetailResult>();
				Log.e("DetailThread", e.getMessage());
			} catch (XmlPullParserException e) {
				result = new LinkedList<DetailResult>();
				Log.e("DetailThread", e.getMessage());
			} catch (IOException e) {
				result = new LinkedList<DetailResult>();
				Log.e("DetailThread", e.getMessage());
			}
		}
		
		msg.obj = result;
		msg.sendToTarget();
	}

}
